<?php
/**
 *
 * @package	Autolink MOD ACP
 *
 */

if (!defined('IN_PHPBB'))
{
    exit;
}

class acp_autolink
{
	var $u_action;
	var $tpl_path;
	var $page_title;
	
	function main($id, $mode)
	{
		global $db, $user, $auth, $template;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
		$user->add_lang('mods/autolink');
		$this->tpl_name = 'acp_autolink';
		$this->page_title = 'ACP_AUTOLINK';
		$action = request_var('action', '', true);
		
		if ($action == 'edit')
		{
			$this->tpl_name = 'acp_autolink_add';
			
			$word_id = (int) request_var('edit_id', 0);
			
			if ($word_id == 0)
			{
				trigger_error($user->lang['AUTOLINK_INVALID_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
			}
			else
			{
				$sql = 'SELECT * FROM ' . AUTOLINK_MOD_WORDS_TABLE . ' WHERE autolink_id = ' . $word_id;
				$result = $db->sql_query($sql);
				$row = $db->sql_fetchrow($result);
				
				if (isset($_POST['submit']))
				{
					$sql_array = array(
						'autolink_word'		=> request_var('autolink_word', '', true),
						'autolink_url'		=> request_var('autolink_url', '', true),
						'autolink_rate'		=> request_var('autolink_rate', '', true),
						'autolink_word_lang'	=> (int) request_var('autolink_lang', '', true)
						);	
						
					$sql = 'UPDATE ' . AUTOLINK_MOD_WORDS_TABLE . ' SET '
						. $db->sql_build_array('UPDATE', $sql_array) . " 
						WHERE autolink_id = $word_id";
						
					if ($db->sql_query($sql))
					{
						trigger_error($user->lang['AUTOLINK_UPDATED'] . adm_back_link($this->u_action));
					}
					else
					{
						trigger_error($user->lang['AUTOLINK_NOT_UPDATED'] . adm_back_link($this->u_action), E_USER_WARNING);
					}
				}
			}

			$template->assign_vars(array(
				'U_W_ACTION'	=> $this->u_action . '&amp;action=edit&amp;edit_id=' . $word_id,
				'S_WORD'	=> $row['autolink_word'],
				'S_URL'		=> $row['autolink_url'],
				'S_RATE'	=> $row['autolink_rate'])
				);
		}
		elseif ($action == 'delete')
		{
			$word_id = request_var('delete_id', 0);
			
			if ($word_id == 0)
			{
				trigger_error($user->lang['AUTOLINK_INVALID_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
			}
			else
			{
				if (confirm_box(true))
				{
					$sql = 'DELETE FROM ' . AUTOLINK_MOD_WORDS_TABLE . ' WHERE autolink_id = ' . $word_id;
					if ($db->sql_query($sql))
					{
						trigger_error($user->lang['AUTOLINK_REMOVED'] . adm_back_link($this->u_action));
					}
					else
					{
						trigger_error($user->lang['AUTOLINK_NOT_REMOVED'] . adm_back_link($this->u_action), E_USER_WARNING);
					}
				}
				else
				{
					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
						'i'		=> $id,
						'mode'		=> $mode,
						'delete_id'	=> $word_id,
						'action'	=> 'delete',
					)));				
				}
			}		
		}
		elseif ($action == 'add')
		{
			$this->tpl_name = 'acp_autolink_add';
			
			if (isset($_POST['submit']))
			{
				$sql_array = array(
					'autolink_id'		=> NULL,
					'autolink_word'		=> request_var('autolink_word', '', true),
					'autolink_url'		=> request_var('autolink_url', '', true),
					'autolink_rate'		=> request_var('autolink_rate', '', true),
					'autolink_word_lang'	=> (int) request_var('autolink_lang', '', true)
					);

				$sql = 'INSERT INTO ' . AUTOLINK_MOD_WORDS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_array);

				if ($db->sql_query($sql))
				{
					add_log('admin', 'LOG_AUTOLINK_WORD_ADDED');
					trigger_error($user->lang['LOG_AUTOLINK_WORD_ADDED'] . adm_back_link($this->u_action));
				}
				else
				{
					trigger_error($user->lang['LOG_AUTOLINK_WORD_NOT_ADDED'] . adm_back_link($this->u_action));
				}
			}

			$sql = 'SELECT * FROM ' . LANG_TABLE;
			$result = $db->sql_query($sql);
			while ($row = $db->sql_fetchrow($result))
			{
				$template->assign_block_vars('autolink_lang_option', array(
					'S_OPTION_VALUE'	=> $row['lang_id'],
					'LANG_NAME'		=> $row['lang_local_name'])
					);
			}
			$db->sql_freeresult($result);	
			
			$template->assign_vars(array(
				'U_W_ACTION'				=> $this->u_action . '&amp;action=add')
				);
		}
		else
		{
			if (isset($_POST['submit']))
			{
				set_config('autolink_replace_times_in_a_post', (int) request_var('autolink_max_post', 1));
				set_config('autolink_max_num_of_a_word', (int) request_var('autolink_max_word', 5)); 
				add_log('admin', 'LOG_AUTOLINK_CONFIG_UPDATED');
				trigger_error($user->lang['LOG_AUTOLINK_CONFIG_UPDATED'] . adm_back_link($this->u_action));
			}
			
			$sql = 'SELECT a.*, l.lang_local_name AS lang_name FROM ' . AUTOLINK_MOD_WORDS_TABLE . ' a
				LEFT JOIN ' . LANG_TABLE . ' l ON l.lang_id = a.autolink_word_lang'; 
			$result = $db->sql_query($sql);
			$i = 1;
			while ($row = $db->sql_fetchrow($result))
			{
				$template->assign_block_vars('autolinks', array(
					'S_ROW_COUNT'	=> $i,
					'NAME'		=> $row['autolink_word'],
					'URL'		=> $row['autolink_url'],
					'RATE'		=> $row['autolink_rate'],
					'LANG'		=> ( (empty($row['lang_name']) || !isset($row['lang_name'])) ? $user->lang['ACP_AUTOLINK_LANG_ALL'] : $row['lang_name'] ),
					'U_EDIT'	=> $this->u_action . '&amp;action=edit&amp;edit_id=' . $row['autolink_id'],
					'U_DELETE'	=> $this->u_action . '&amp;action=delete&amp;delete_id=' . $row['autolink_id']
					)
				);
				$i++;
			}
			$db->sql_freeresult($result);
			
			$template->assign_vars(array(
				'S_WORDS_MAX_POST'		=> (int) $config['autolink_replace_times_in_a_post'],
				'S_MAX_WORD'			=> (int) $config['autolink_max_num_of_a_word'])
				);
		}
		
		$template->assign_vars(array(
			'U_ADD'				=> $this->u_action . '&amp;action=add',
			'U_ACTION'			=> $this->u_action)
		);		
	}
}

?>
